package cn.edu.lcvc.ebuy_lyj.web.controler.backstage;


import cn.edu.lcvc.ebuy_lyj.model.Admin;
import cn.edu.lcvc.ebuy_lyj.service.AdminBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/backstage/adminmanage")
public class GetAdminListController {
    @Autowired
    private AdminBean adminBean;//创建并初始化业务Bean


    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public String getAdminList(HttpServletRequest req) {
        System.out.println("hello!");
        List<Admin> admin=adminBean.findAll();
        req.setAttribute("admin",admin);
        return "/jsp/backstage/adminmanage/adminmanage.jsp";

    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String DeleteAdmin(Integer id){
        adminBean.deleteAdmin(id);
        System.out.println(id);
        return "/backstage/adminmanage/all";
    }
    @RequestMapping(value = "/toAdd",method = RequestMethod.GET)
    public String toAdd(){

        return "/jsp/backstage/adminmanage/Adminadd.jsp";
    }
    @RequestMapping(value = "/doAdd",method = RequestMethod.GET)
    public String doAdd(String username, String password, String name, int sex, boolean unlocked,HttpServletRequest req){
//        System.out.println(createTime);
        String yy="";
        if (adminBean.isEntryUsername(username)){
            yy+="用户已存在";
            req.setAttribute("alert_msg",yy);
            return "/jsp/backstage/adminmanage/Adminadd.jsp";
        }
        adminBean.adminAdd(username,password,name,sex,unlocked);
        req.setAttribute("alert_msg","用户添加成功");
        return "/backstage/adminmanage/all";

    }
    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Integer id,HttpServletRequest req){
        Admin admin=adminBean.getAdminById(id);
        req.setAttribute("originalAdmin",admin);
        return "/jsp/backstage/adminmanage/AdminUpdate.jsp";
    }
    @RequestMapping(value = "/doUpdate",method = RequestMethod.GET)
    public String doUpdate(Integer id,String username, String name, int sex, boolean unlocked,HttpServletRequest req){
//        System.out.println(createTime);
        String yy="";

        if (adminBean.isEntryUsername(username)){
            yy+="用户已存在";
            req.setAttribute("alert_msg",yy);
            return "/backstage/adminmanage/toUpdate";
        }
        adminBean.updateAdmin(id,username,name,sex,unlocked);
        req.setAttribute("alert_msg","用户修改成功");
        return "/backstage/adminmanage/all";

    }
}
